{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4563063616\n",
      "4563063616\n",
      "<class '__main__.A'>\n",
      "4563064480\n",
      "4563063808\n"
     ]
    }
   ],
   "source": [
    "# 1. 用装饰器\n",
    "from functools import wraps\n",
    "import threading\n",
    "\n",
    "def singleton(cls):\n",
    "    map_ = {}\n",
    "    @wraps(cls)\n",
    "    def wrapper(*args, **kwargs):\n",
    "        with threading.RLock() as lock:\n",
    "            if cls not in map_:\n",
    "                map_[cls] = cls(*args, **kwargs)\n",
    "        return map_[cls]\n",
    "    return wrapper\n",
    "\n",
    "@singleton\n",
    "class A():\n",
    "    pass\n",
    "\n",
    "a = A()\n",
    "b = A()\n",
    "print(id(a))\n",
    "print(id(b))\n",
    "\n",
    "SourceA = A.__wrapped__  # 获取装饰器的类\n",
    "print(SourceA)\n",
    "a = SourceA()\n",
    "b = SourceA()\n",
    "print(id(a))\n",
    "print(id(b))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "singleton init\n",
      "singleton init\n",
      "singleton call\n",
      "A init\n",
      "singleton call\n",
      "singleton call\n",
      "singleton call\n",
      "4563377360\n",
      "4563377360\n",
      "4563377264\n",
      "4563377264\n",
      "1 2\n",
      "3 4\n"
     ]
    }
   ],
   "source": [
    "# 2. 用元类\n",
    "from pyparsing import Any\n",
    "\n",
    "\n",
    "class SingleTon(type):\n",
    "    def __init__(cls, *args, **kwargs):\n",
    "        print('singleton init')\n",
    "        cls.__instance = None\n",
    "        # super().__init__(*args, **kwargs)\n",
    "    def __call__(cls, *args, **kwds):\n",
    "        print('singleton call')\n",
    "        if not cls.__instance:\n",
    "            cls.__instance = type.__call__(cls, *args, **kwds)\n",
    "        return cls.__instance\n",
    "\n",
    "class A(metaclass=SingleTon):\n",
    "    def __init__(self) -> None:\n",
    "        print('A init')\n",
    "        \n",
    "        self.x = 1\n",
    "        self.y = 2\n",
    "    def __call__(self, *args: Any, **kwds: Any) -> Any:\n",
    "        print('A call')\n",
    "\n",
    "class B(metaclass=SingleTon):\n",
    "    def __init__(self) -> None:\n",
    "        self.x = 3\n",
    "        self.y = 4\n",
    "a1 = A()\n",
    "a2 = A()\n",
    "b1 = B()\n",
    "b2 = B()\n",
    "\n",
    "\n",
    "print(id(a1))\n",
    "print(id(a2))\n",
    "print(id(b1))\n",
    "print(id(b2))\n",
    "print(a1.x, a1.y)\n",
    "print(b1.x, b1.y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('opencv')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "b04e532e6ce11d72ca7f2113c964d7a5531c18c5e6d3e97b1c42c6f9a3724305"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
